const state = {
  visitedViews: [], // 访问过的页面
}

const mutations = {
  // 点击左边菜单增加 router-link 方法
  ADD_VIEW(state, view) {
    state.visitedViews.push({
      name: view.name,
      path: view.path,
      fullPath: view.fullPath
    })
  },
  // menu 选择框 - 关闭当前
  CLOSE_SELECTED_TAG(state, view) {
    let index = state.visitedViews.findIndex(item => {
      return item.path === view.path
    })
    state.visitedViews.splice(index, 1)
  },
  // menu 选择框 - 关闭其他
  CLOSE_OTHERS_TAGS(state, view) {
    let result = state.visitedViews.filter(item => {
      return item.path === view.path || item.name == '首页'
    })
    state.visitedViews = result
  },
  // menu 选择框 - 全部关闭
  CLOSE_ALL_TAGS(state) {
    let result = state.visitedViews.filter(item => {
      return item.name == '首页'
    })
    state.visitedViews = result
  }
}

const actions = {
  // 点击左边菜单增加 router-link 方法
  addView(addView, view) {
    addView.commit('ADD_VIEW', view)
  },
  // menu 选择框 - 关闭当前
  closeSelectedTag(closeSelectedTag, view) {
    closeSelectedTag.commit('CLOSE_SELECTED_TAG', view)
  },
  // menu 选择框 - 关闭其他
  closeOthersTags(closeOthersTags, view) {
    closeOthersTags.commit('CLOSE_OTHERS_TAGS', view)
  },
  // menu 选择框 - 全部关闭
  closeAllTags(closeAllTags, view) {
    closeAllTags.commit('CLOSE_ALL_TAGS', view)
  },
}

// 将仓库导出
export default {
  namespaced: true,
  state,
  mutations,
  actions
}